package com.xuedakun.xasdk.net.volley;

import com.android.volley.*;
import com.android.volley.RequestQueue.RequestFilter;
import com.android.volley.RequestQueue.RequestFinishedListener;
import com.android.volley.toolbox.DiskBasedCache;
import com.android.volley.toolbox.Volley;
import com.xuedakun.xasdk.XApplication;
import com.xuedakun.xasdk.XLog;

/**
 * Created by xuedakun on 2017/6/3 19:34
 * volley的操作
 *
 * @version : v1.0
 * @project : xasdk
 * @Email : dakun611@Gmail.com
 */

public class VolleyRequest {

    private static final String LOG_TAG = "VolleyRequest";

    private static VolleyRequest instance;

    private RequestQueue requestQueue;

    private VolleyRequest() {
        requestQueue = Volley.newRequestQueue(XApplication.getContext());
    }

    public static VolleyRequest getInstance() {
        synchronized (VolleyRequest.class) {
            if (instance == null) {
                instance = new VolleyRequest();
            }
            return instance;
        }
    }

    void addRequest(Request request) {
        requestQueue.add(request);
    }

    /**
     * 通过tag删除对应的全部request
     *
     * @param tag
     */
    public void cancelRequest(Object tag) {
        requestQueue.cancelAll(tag);
    }

    /**
     * 通过过滤器删除对应的全部request
     *
     * @param filter
     */
    public void cancelRequest(RequestFilter filter) {
        requestQueue.cancelAll(filter);
    }

    /**
     * 添加请求信息
     * @param requestFinishedListener
     */
    public void addRequestFinishedListener(RequestFinishedListener requestFinishedListener) {
        requestQueue.addRequestFinishedListener(requestFinishedListener);
    }

    /**
     * 获取volley的缓存
     * @return
     */
    public DiskBasedCache getVolleyCache() {
        return (DiskBasedCache) requestQueue.getCache();
    }
}
